﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChinaDynastyTours.Helpers
{
    class MoneyHelper
    {
        /// <summary>
        /// 货币金额中文大写转换
        /// </summary>
        /// <param name="n1">货币金额数字</param>
        /// <returns>中文大写</returns>
        public static string ChMoney(decimal n1)
        {
            //转换结果
            string convertResult = "";
            //货币数字字符串
            string tMoney = "";
            //小数点位置
            int tn = 0;
            //每位数字
            string t1 = "";
            //小数位大写字符串
            string s1 = "";
            //个十百千位大写字符串
            string s2 = "";
            //万位以上大写字符串
            string s3 = "";
            //转换临时字符串
            string st = "";
            //如为0返回空字符串
            if (n1 == 0)
            {
                convertResult = "";
                return convertResult;
            }
            //如为负数，返回负数大写字符串
            if (n1 < 0)
            {
                convertResult = "负" + ChMoney(System.Math.Abs(n1));
                return convertResult;
            }

            tMoney = n1.ToString().Trim();
            tn = tMoney.IndexOf(".");
            //生成小数位大写
            if (tn != -1)
            {
                st = tMoney.Substring(tn + 1);
                if (st != "")
                {
                    t1 = st.Substring(0, 1);
                    st = st.Substring(1);
                    if (t1 != "0")
                    {
                        s1 += CCh(int.Parse(t1)) + "角";
                    }
                    if (st != "")
                    {
                        t1 = st.Substring(0, 1);
                        s1 += CCh(int.Parse(t1)) + "分";
                    }
                }
                st = tMoney.Substring(0, tn);
            }
            else
            {
                st = tMoney;
            }

            //生成个十百千位大写
            if (st != "")
            {
                t1 = st.Substring(st.Length - 1);
                st = st.Substring(0, st.Length - 1);
                s2 += CCh(int.Parse(t1));
            }
            if (st != "")
            {
                t1 = st.Substring(st.Length - 1);
                st = st.Substring(0, st.Length - 1);
                if (t1 != "0")
                {
                    s2 = CCh(int.Parse(t1)) + "拾" + s2;
                }
                else if (s2.Substring(0, 1) != "零")
                {
                    s2 = "零" + s2;
                }
            }

            if (st != "")
            {
                t1 = st.Substring(st.Length - 1);
                st = st.Substring(0, st.Length - 1);
                if (t1 != "0")
                {
                    s2 = CCh(int.Parse(t1)) + "佰" + s2;
                }
                else if (s2.Substring(0, 1) != "零")
                {
                    s2 = "零" + s2;
                }
            }
            if (st != "")
            {
                t1 = st.Substring(st.Length - 1);
                st = st.Substring(0, st.Length - 1);
                if (t1 != "0")
                {
                    s2 = CCh(int.Parse(t1)) + "仟" + s2;
                }
                else if (s2.Substring(0, 1) != "零")
                {
                    s2 = "零" + s2;
                }
            }
            //生成万位以上大写，最大为千万位
            if (st != "")
            {
                t1 = st.Substring(st.Length - 1);
                st = st.Substring(0, st.Length - 1);
                s3 += CCh(int.Parse(t1));
            }
            if (st != "")
            {
                t1 = st.Substring(st.Length - 1);
                st = st.Substring(0, st.Length - 1);
                if (t1 != "0")
                {
                    s3 = CCh(int.Parse(t1)) + "拾" + s3;
                }
                else if (s3.Substring(0, 1) != "零")
                {
                    s3 = "零" + s3;
                }
            }
            if (st != "")
            {
                t1 = st.Substring(st.Length - 1);
                st = st.Substring(0, st.Length - 1);
                if (t1 != "0")
                {
                    s3 = CCh(int.Parse(t1)) + "佰" + s3;
                }
                else if (s3.Substring(0, 1) != "零")
                {
                    s3 = "零" + s3;
                }
            }
            if (st != "")
            {
                t1 = st.Substring(st.Length - 1);
                st = st.Substring(0, st.Length - 1);
                if (t1 != "0")
                {
                    s3 = CCh(int.Parse(t1)) + "仟" + s3;
                }
                else if (s3.Substring(0, 1) != "零")
                {
                    s3 = "零" + s3;
                }
            }
            if (s2.Substring(s2.Length - 1) == "零")
                s2 = s2.Substring(0, s2.Length - 1);
            if (s3.Length > 0)
            {
                if (s3.Substring(s3.Length - 1) == "零")
                    s3 = s3.Substring(0, s3.Length - 1);
                s3 += "万";
            }
            //生成转换结果
            convertResult = s3 + s2 == "" ? s1 : s3 + s2 + "元" + s1;
            return convertResult;
        }
        /// <summary>
        /// 阿拉伯数字转换为中文数字
        /// </summary>
        /// <param name="n2">单个阿拉伯数字</param>
        /// <returns>中文数字</returns>
        public static string CCh(int n2)
        {
            string chnNum = "";
            switch (n2)
            {
                case 0:
                    chnNum = "零";
                    break;
                case 1:
                    chnNum = "壹";
                    break;
                case 2:
                    chnNum = "贰";
                    break;
                case 3:
                    chnNum = "叁";
                    break;
                case 4:
                    chnNum = "肆";
                    break;
                case 5:
                    chnNum = "伍";
                    break;
                case 6:
                    chnNum = "陆";
                    break;
                case 7:
                    chnNum = "柒";
                    break;
                case 8:
                    chnNum = "捌";
                    break;
                case 9:
                    chnNum = "玖";
                    break;
            }
            return chnNum;
        }

    }
}
